﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Common.Logging;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace WuDada.Core.Generic.Util
{
    /// <summary>
    /// 用於圖片的縮小util
    /// </summary>
    public class ImageUtil
    {
        /// <summary>
        /// 縮小圖片
        /// </summary>
        /// <param name="oldPicPath">原圖片位置</param>
        /// <param name="newPicPath">新圖片位置</param>
        /// <param name="newHeight">欲縮小後的寬度</param>
        /// <param name="imgType">影像類型</param>
        public static void ToSmallPic(string oldPicPath, string newPicPath, int newHeight, int newWidth, ImageFormat imgType)
        {
            ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

            byte[] imgByte = GetImageByteArr(new Bitmap(oldPicPath), imgType);
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.Write(imgByte, 0, imgByte.Length);
            System.Drawing.Image imagen = System.Drawing.Image.FromStream(memoryStream);

            ImageResize ir = new ImageResize();

            ir.File = imagen;
            ir.Height = newHeight;
            ir.Width = newWidth;
            log.Debug("============>" + newPicPath);
            ir.GetThumbnail().Save(newPicPath, imgType);
            imagen.Dispose();
            memoryStream.Dispose();

        }

        /// <summary>
        /// 按比例縮小圖片(BMP)，自動計算高度
        /// </summary>
        /// <param name="oldPicPath">原圖片位置</param>
        /// <param name="newPicPath">新圖片位置</param>
        /// <param name="newHeight">欲縮小後的寬度</param>
        /// <param name="imgType">影像類型</param>
        public static void ToSmallPicInWidth(string oldPicPath, string newPicPath, int newWidth, ImageFormat imgType)
        {
            System.Drawing.Image imagen = System.Drawing.Image.FromFile(oldPicPath);

            if (imagen.Width > newWidth)
            {
                int newHeight = (newWidth / imagen.Width) * imagen.Height;

                ImageResize ir = new ImageResize();

                ir.File = imagen;
                ir.Height = newHeight;
                ir.Width = newWidth;

                ir.GetThumbnail().Save(newPicPath, imgType);                
            }
            imagen.Dispose();
        }

        /// <summary>
        /// 按比例縮小圖片，自動計算寬度
        /// </summary>
        /// <param name="oldPicPath">原圖片位置</param>
        /// <param name="newPicPath">新圖片位置</param>
        /// <param name="newHeight">欲縮小後的高度</param>
        /// <param name="imgType">影像類型</param>
        public static void ToSmallPicInHeight(string oldPicPath, string newPicPath, int newHeight, ImageFormat imgType)
        {
            System.Drawing.Image imagen = System.Drawing.Image.FromFile(oldPicPath);

            if (imagen.Height > newHeight)
            {
                int newWidth = (newHeight / imagen.Width) * imagen.Width;

                ImageResize ir = new ImageResize();

                ir.File = imagen;
                ir.Height = newHeight;
                ir.Width = newWidth;

                ir.GetThumbnail().Save(newPicPath, imgType);                
            }
            imagen.Dispose();
        }

        private static byte[] GetImageByteArr(System.Drawing.Image img, ImageFormat imgType)
        {
            byte[] ImgByte;
            using (MemoryStream stream = new MemoryStream())
            {
                img.Save(stream, imgType);
                ImgByte = stream.ToArray();
            }
            return ImgByte;
        }
    }
}
